window.onload = function () {
    loadSkeleton();
    document.getElementById('importButton').addEventListener('click', importBooks);
    // 页面加载时显示 books 目录内容
    displayBooksDirectory();
    // 页面加载时显示已导入的书籍列表
    displayImportedBooks();
};

function importBooks() {
    const bookFolderInput = document.getElementById('bookFolder');
    const files = bookFolderInput.files;

    if (files.length === 0) {
        alert('请选择一个图书文件夹');
        return;
    }

    // 获取已导入的书籍列表
    let importedBooks = JSON.parse(localStorage.getItem('importedBooks')) || [];
    let importedCount = 0;

    // 不再显示每个文件的导入过程，改为静默导入
    for (let i = 0; i < files.length; i++) {
        const file = files[i];
        // 将书籍信息添加到已导入的书籍列表中
        if (!importedBooks.some(book => book.name === file.name)) {
            importedBooks.push({ name: file.name });
            importedCount++;
        }
    }

    // 将更新后的已导入书籍列表保存到本地存储
    localStorage.setItem('importedBooks', JSON.stringify(importedBooks));

    // 显示已导入的书籍列表
    displayImportedBooks();
}

function openBook(bookName) {
    // 假设书籍文件存储在 books 目录下
    const bookPath = `books/${bookName}`;
    const link = document.createElement('a');
    link.href = bookPath;
    link.target = '_blank';
    link.click();
}

function displayImportedBooks() {
    const importedBooksList = document.getElementById('importedBooksList');
    if (importedBooksList) {
        importedBooksList.innerHTML = '';
        // 获取已导入的书籍列表
        const importedBooks = JSON.parse(localStorage.getItem('importedBooks')) || [];

        importedBooks.forEach(book => {
            const li = document.createElement('li');
            li.textContent = book.name;
            li.style.cursor = 'pointer';

            li.addEventListener('click', () => {
                openBook(book.name);
            });

            // 添加删除按钮
            const deleteButton = document.createElement('button');
            deleteButton.textContent = '删除';
            deleteButton.classList.add('btn', 'btn-danger', 'btn-sm', 'ms-2');
            deleteButton.addEventListener('click', () => {
                if (confirm('确定要删除这本书吗？')) {
                    deleteBook(book.name);
                }
            });

            li.appendChild(deleteButton);
            importedBooksList.appendChild(li);
        });
    }
}

function deleteBook(bookName) {
    let importedBooks = JSON.parse(localStorage.getItem('importedBooks')) || [];
    importedBooks = importedBooks.filter(book => book.name !== bookName);
    localStorage.setItem('importedBooks', JSON.stringify(importedBooks));
    displayImportedBooks();
}

function displayBooksDirectory() {
    const booksDirectoryList = document.getElementById('booksDirectoryList');
    booksDirectoryList.innerHTML = '';

    
    const booksDirectoryContent = [];

    booksDirectoryContent.forEach(item => {
        const li = document.createElement('li');
        li.textContent = item;
        li.style.cursor = 'pointer';

        li.addEventListener('click', () => {
            openBook(item);
        });

        booksDirectoryList.appendChild(li);
    });
} 
